<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use  App\Model\Type;

class TypeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       
        $type = DB::table('shop_type')->select(['id','name','pid','path',])->orderBy(DB::raw('concat(path, id)'))->paginate(10);

        return view('Admin/TypeAdmin/type',['type'=>$type]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
       
        return view('Admin/TypeAdmin/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //stotr(储存)
        
        $name = $request->input('name');
        $type = DB::table('shop_type')->select(['id'])->where('name',$name)->first();
       
        if($type){
             return back()->with('errorTip','该父类别已存在');
        }
        $pid = 0;
        $path = $pid.',';
        if(!$name){
            return back()->with('errorTip','父类别不能为空');
        }
        
        $insert = Type::insertGetId([
                   
                    'name' => $name,
                    'pid' =>$pid,
                    'path'=>$path,
         ]);
       
        if($insert){
            return redirect('/admin/Type/index')->with('msg','父类别添加成功');
        }else{
            return back()->with('errorTip','父类别添加失败');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
       
        $oneType = DB::table('shop_type')->select(['id','name'])->where('id',$id)->first();
        
        return view('Admin/TypeAdmin/show',['type'=>$oneType]);

    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //添加子分类 edit方法是拿到父类别的值
        
        $oneType = DB::table('shop_type')->select(['id','name'])->where('id',$id)->first();
       
        return view('Admin/TypeAdmin/edit',['type'=>$oneType]);

    }
    //samllstore用来添加子类别
    public function smallstore(Request $request,$id)
    {
        //stotr(储存)

       
        $oneType = DB::table('shop_type')->select(['id','name','pid','path'])->where('id',$id)->first();
        
        $name = $request->name;
       if(!$name){
        return back()->with('errorTip','子类别不能为空');
       }

        $pid = $oneType->id;
        $path = $oneType->path.$pid.',';
       
        
        $insert = Type::insertGetId([
                   
                    'name'=>$name,
                    'pid' =>$pid,
                    'path'=>$path,
         ]);
       
        if($insert){
            return redirect('/admin/Type/index')->with('msg','子类别添加成功');
        }else{
            return back()->with('errorTip','添加失败');
        }


    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $name = $request->input('name');
         if(!$name){
        return back()->with('errorTip','父类别名称不能为空');
       }

         $bool = DB::table('shop_type')
                ->where('id',$id)
                ->update([
                        
                        'name'=>$name,
                        ]);
               

                if($bool!==0) {
                        
                  return redirect('/admin/Type/index')->with('msg','修改成功');
                     
                }
                 return back()->with('errorTip','修改失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
        
        $oneType = DB::table('shop_type')->select(['id'])->where('pid',$id)->first();
       
       if($oneType){
        return redirect('/admin/Type/index')->with('errorTip','该父分类还存有子分类，不能被删除');

       }
        
         $bool = DB::table('shop_type')->where('id',$id)->delete();

                
                 return redirect('/admin/Type/index')->with('msg','删除成功');
    }
}
